<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    PriceController
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * @category   Zend
 * @package    PriceController
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_PriceController extends Zend_Controller_Action
{

    /**
     * The logger
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * The auth data
     * @var Zend_Auth
     */
    protected $_auth;

    /**
     * The AJAX flag
     * @var boolean
     */
    protected $_isAjax;

    /**
     * Initialize the controller
     */
    public function init()
    {
        // Get logger
        $this->_logger = Zend_Registry::get('log');

        // Set admin layout
        $this->_helper->layout()->setLayout('admin');

        // Get language from locale
        $locale = Zend_Registry::get('Zend_Locale');
        $this->view->language = $locale->getLanguage();

        // Get language ID
        $languageModel = new Admin_Model_Language();
        $this->_languageId = $languageModel->findIdByShortCode($locale->getLanguage());

        // Chect authenctication
        $this->_auth = Zend_Auth::getInstance();
        if (!$this->_auth->hasIdentity()) {
            $this->_redirect('/' . $this->view->language . '/admin/login');
        }

        // Check AJAX
        if ($this->getRequest()->isXmlHttpRequest()) {

            // If AJAX - turn of auto render
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
            $this->_isAjax = true;
        } else {
            // Get current url without language
            $this->view->currentUrl = str_replace('/' . $locale->getLanguage(), '', $this->_request->getRequestUri());

            $this->view->shopName = Zend_Registry::get('config')->shopName;
        }
    }

    /**
     * Action for index
     */
    public function indexAction()
    {

        // Initialize filters for orders
        $name = null;
        $productId = null;
        $productMpn = null;
        $manufacturerName = null;
        $distributorName = null;
        $updatedFrom = null;
        $updatedTo = null;
        $priceFrom = null;
        $priceTo = null;
        $publicPriceFrom = null;
        $publicPriceTo = null;
        $sort = null;
        $direction = null;
        $page = 1;
        $rowCount = 20;

        // Initialize form for filter
        $form = new Admin_Model_FormDistributorPrices();
        $form->setAction('/' . $this->view->language . '/admin/distributor_price/index');

        // Get request params
        if ($this->getRequest()->isPost() && $form->isValid($data = $this->_request->getPost())) {
            $name = $data['name'];
            $productId = $data['product_id'];
            $productMpn = $data['product_mpn'];
            $manufacturerName = $data['manufacturer_name'];
            $distributorName = $data['distributor_name'];
            $updatedFrom = $data['updated_from'];
            $updatedTo = $data['updated_to'];
            $priceFrom = $data['price_from'];
            $priceTo = $data['price_to'];
            $publicPriceFrom = $data['public_price_from'];
            $publicPriceTo = $data['public_price_to'];
            $sort = $data['sort'];
            $direction = $data['direction'];
            $page = $data['page'];
            $rowCount = $data['rows'];
        }

        // Get invoices from database
        $distributorPriceModel = new Admin_Model_DistributorProduct();
        $this->view->distributorPrices = $distributorPriceModel->getAllByFilter($name, $productId, $productMpn, $manufacturerName, $distributorName, $updatedFrom, $updatedTo, $priceFrom, $priceTo, $publicPriceFrom, $publicPriceTo, $sort, $direction, $page, $rowCount);

        // Get page count
        $recordsCount = $distributorPriceModel->getCountByFilter($name, $productId, $productMpn, $manufacturerName, $distributorName, $updatedFrom, $updatedTo, $priceFrom, $priceTo, $publicPriceFrom, $publicPriceTo);
        $form->records_count->setValue($recordsCount);
        $form->pages_count->setValue(ceil($recordsCount / $rowCount));

        // Add form to view
        $this->view->formDistributorPrices = $form;

        // Render AJAX content
        if ($this->_isAjax) {
            echo $this->view->render('/price/_filtered.phtml');
        }

        // Get time of execution
        $startTime = Zend_Registry::get('startTime');
        $totalTime = round(microtime(true) - $startTime, 3);
        $this->_logger->debug(__METHOD__ . " spent $totalTime s");
    }

}

